<?php

namespace app\service;
use app\model\FinancialLog;
use think\facade\App;
use think\facade\Log;
use app\model\Payment;
use think\validate\ValidateRule;


class PaymentService extends BaseService
{
    protected function setModel(): string
    {
        return Payment::class;
    }


    public function detail($id)
    {
        return $this->getModel()->with(["member" => function ($query) {
            $query->field("id,email");
        }, 'coin' => function ($query) {
            $query->field("id,coin_name");
        }])->field("id,pay_img,amount,pay_no,member_id,coin_id,create_at,status")
            ->where(["id" => $id])
            ->find();
    }

    public function audit($id, $remark, $status)
    {
        return $this->save(["status" => $status, 'remark' => $remark, 'id' => $id]);
    }

    public function setCommission($mid,$amount,$coinId)
    {
        //获取返佣比例
        $memberService = App::make(MemberService::class);
        $agents = $memberService->getAgentMapByMid($mid);
        if (!$agents) {
            return true;
        }
        $level =0;
        foreach ($agents as $key=>$user) {
            if ($user["percent"]<=0) {
                continue;
            }
            $financialLogService = App::make(FinancialLogService::class);
            $level = $level+1;
            $commission = $amount*$user["percent"]/100;
            $remark =  $level . '代充值返佣，来自下级用户'. $user["realname"]. '返佣金额: ' . $commission;
            $financialLogService->flowLog($key,$commission,$coinId,0,FinancialLog::COMMISSION, $remark);
        }
        return true;
    }
}